package com.lhr.common.web;

import java.text.ParseException;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateUtils;
import org.springframework.core.convert.converter.Converter;

public class DateConverter implements Converter<String, Date> {
	private String[] parsePattern = new String[] { "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd" };

	@Override
	public Date convert(String source) {
		if (StringUtils.isBlank(source)) {
			return null;
		}
		try {
			return DateUtils.parseDate(source, parsePattern);
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}
	}

	public String[] getParsePattern() {
		return parsePattern;
	}

	public void setParsePattern(String[] parsePattern) {
		this.parsePattern = parsePattern;
	}

}
